<?php

// SECURITY CHECK
	if(!isset($cosa)) die();
	if($cosa->system('installed')) die('System seems to installed already.');

// INSTALL
	if(!isset($_POST['install'])) {
		
		include ROOT_PATH.'/kernel/lib/tpl/install/form.php';
		
	} else {
		
		// SET VARS
			$sqls = array();
			$p = $_POST;
			$uname = addslashes($p['uname']);
			$email = addslashes($p['email']);
			$pass = addslashes(md5($p['pass']));
			$name_f = addslashes($p['name_f']);
			$name_l = addslashes($p['name_l']);
			$charset = addslashes($p['charset']);
			$site_title = addslashes($p['site_title']);
			$base_url = addslashes($p['base_url']);
			$site_url = addslashes($p['site_url']);
			$now_dt = date('Y-m-d H:i:s');
		
		// CREATE DOMAINS QUERIES
			$sqls['domains'] = array();
			$sqls['domains'][] = "CREATE TABLE `".$db->table('domains')."` (
								  `id` int(11) NOT NULL auto_increment,
								  `name` varchar(255) NOT NULL,
								  `short` varchar(5) NOT NULL,
								  `active` enum('1','0') NOT NULL default '1',
								  `default` enum('1','0') NOT NULL default '1',
								  PRIMARY KEY  (`id`)
								) ENGINE=MyISAM
								";
			$sqls['domains'][] = "insert into `".$db->table('domains')."` values('1','Default','def','1','1');";
		
		// CREATE LANGUAGE QUERIES
			$sqls['language'] = array();
			$sqls['language'][] = "CREATE TABLE `".$db->table('languages')."` (
									  `id` int(11) NOT NULL auto_increment,
									  `domain_id` int(11) NOT NULL,
									  `name` varchar(255) NOT NULL,
									  `short` varchar(4) NOT NULL,
									  `active` enum('1','0') NOT NULL default '1',
									  `default` enum('1','0') NOT NULL default '1',
									  PRIMARY KEY  (`id`)
									) ENGINE=MyISAM;
									";
			$sqls['language'][] = "insert into `".$db->table('languages')."` values('1','1','English','en','1','1');";
			$sqls['language'][] = "CREATE TABLE `".$db->table('languages_perm')."` (
									  `language_id` int(11) NOT NULL,
									  `users_group_id` int(11) NOT NULL,
									  PRIMARY KEY  (`language_id`,`users_group_id`),
									  KEY `group_id, language_id` (`users_group_id`,`language_id`)
									) ENGINE=MyISAM
									";
			$sqls['language'][] = "insert into `".$db->table('languages_perm')."` values('1','1');";
		
		// CREATE MODULES QUERIES
			$sqls['modules'] = array();
			$sqls['modules'][] = "CREATE TABLE `".$db->table('modules')."` (
									  `id` int(11) NOT NULL auto_increment,
									  `name` varchar(255) NOT NULL,
									  `description` varchar(255) NOT NULL,
									  `version` varchar(10) NOT NULL,
									  `author` varchar(255) NOT NULL,
									  `author_url` varchar(255) NOT NULL,
									  `module_url` varchar(255) NOT NULL,
									  `path` varchar(255) NOT NULL,
									  `script` varchar(255) NOT NULL,
									  `style` varchar(255) NOT NULL,
									  `installed_at` datetime NOT NULL,
									  `installed_by` int(11) NOT NULL,
									  `type` varchar(255) NOT NULL default '',
									  PRIMARY KEY  (`id`)
									) ENGINE=MyISAM
									";
			$sqls['modules'][] = "insert into `".$db->table('modules')."` values
									 ('1','Dashboard','Standard dashboard module for Cosa admin','1.0.0','Stijn Van Minnebruggen','www.donotfold.be','www.donotfold.be/projects/cosa','dashboard','','css/styles.css','".$now_dt."','1','hidden'),
									 ('2','System','Standard system module for Cosa admin','1.0.0','Stijn Van Minnebruggen','www.donotfold.be','www.donotfold.be/projects/cosa','system','','','".$now_dt."','1','system'),
									 ('3','File Manager','Standard file manager module for Cosa admin','1.0.0','Stijn Van Minnebruggen','www.donotfold.be','www.donotfold.be/projects/cosa','filemanager','js/scripts.js','css/styles.css','".$now_dt."','1','system'),
									 ('4','Users','Standard users module for Cosa admin','1.0.0','Stijn Van Minnebruggen','www.donotfold.be','www.donotfold.be/projects/cosa','users','','','".$now_dt."','1','system');
									 ";
			$sqls['modules'][] = "CREATE TABLE `".$db->table('modules_perm')."` (
									  `module_id` int(11) NOT NULL,
									  `users_group_id` int(11) NOT NULL,
									  PRIMARY KEY  (`module_id`,`users_group_id`),
									  KEY `group_id, module_id` (`users_group_id`,`module_id`)
									) ENGINE=MyISAM
									";
			$sqls['modules'][] = "insert into `".$db->table('modules_perm')."` values('1','1'), ('2','1'), ('3','1'), ('4','1');";
		
		// CREATE PATHS QUERIES
			$sqls['paths'] = array();
			$sqls['paths'][] = "CREATE TABLE `".$db->table('paths')."` (
								  `id` int(11) NOT NULL auto_increment,
								  `table` varchar(255) NOT NULL,
								  `item_id` int(11) NOT NULL,
								  `language_id` int(11) NOT NULL,
								  `path` varchar(255) NOT NULL,
								  PRIMARY KEY  (`id`)
								) ENGINE=MyISAM
								";
		
		// CREATE TAGS QUERIES
			$sqls['tags'] = array();
			$sqls['tags'][] = "CREATE TABLE `".$db->table('tags')."` (
								  `id` int(11) NOT NULL auto_increment,
								  `tag` varchar(255) NOT NULL,
								  `count` int(11) NOT NULL,
								  `language_id` int(11) NOT NULL,
								  PRIMARY KEY  (`id`)
								) ENGINE=MyISAM
								";
			$sqls['tags'][] = "CREATE TABLE `".$db->table('tags_rel')."` (
								  `tag_id` int(11) NOT NULL,
								  `table` varchar(255) NOT NULL,
								  `item_id` int(11) NOT NULL,
								  PRIMARY KEY  (`tag_id`,`table`,`item_id`)
								) ENGINE=MyISAM
								";
		
		// CREATE PLUGINS QUERIES
			$sqls['plugins'] = array();
			$sqls['plugins'][] = "CREATE TABLE `".$db->table('plugins')."` (
									  `id` int(11) NOT NULL auto_increment,
									  `name` varchar(255) NOT NULL,
									  `description` varchar(255) NOT NULL,
									  `version` varchar(10) NOT NULL,
									  `author` varchar(255) NOT NULL,
									  `author_url` varchar(255) NOT NULL,
									  `plugin_url` varchar(255) NOT NULL,
									  `path` varchar(255) NOT NULL,
									  `installed_at` datetime NOT NULL,
									  `installed_by` int(11) NOT NULL,
									  PRIMARY KEY  (`id`)
									) ENGINE=MyISAM
									";
			$sqls['plugins'][] = "insert into `".$db->table('plugins')."` values('1','TinyMCE','Rich html editor plugin','1.0.0','Stijn Van Minnebruggen','www.donotfold.be','www.donotfold.be/projects/cosa','tinymce','".$now_dt."','1');";
		
		// CREATE USERS QUERIES
			$sqls['users'] = array();
			$sqls['users'][] = "CREATE TABLE `".$db->table('users')."` (
								  `id` int(11) NOT NULL auto_increment,
								  `uname` varchar(255) NOT NULL,
								  `email` varchar(255) NOT NULL,
								  `pass` varchar(255) NOT NULL,
								  `name_f` varchar(255) NOT NULL,
								  `name_l` varchar(255) NOT NULL,
								  `group_id` int(11) NOT NULL,
								  `language_id` int(11) NOT NULL,
								  `time_created` datetime NOT NULL,
								  `time_modified` datetime NOT NULL,
								  `time_lastlogin` datetime NOT NULL,
								  PRIMARY KEY  (`id`)
								) ENGINE=MyISAM
								";
			$sqls['users'][] = "insert into `".$db->table('users')."` values
								('1','".$uname."','".$email."','".$pass."','".$name_f."','".$name_l."','1','1','".$now_dt."','".$now_dt."','".$now_dt."');
								";
			$sqls['users'][] = "CREATE TABLE `".$db->table('users_groups')."` (
								  `id` int(11) NOT NULL auto_increment,
								  `name` varchar(255) NOT NULL,
								  PRIMARY KEY  (`id`)
								) ENGINE=MyISAM
								";
			$sqls['users'][] = "insert into `".$db->table('users_groups')."` values('1','Admin');";
		
		// CREATE SETTINGS QUERIES
			$sqls['settings'] = array();
			$sqls['settings'][] = "CREATE TABLE `".$db->table('settings')."` (
									  `id` int(11) NOT NULL auto_increment,
									  `key` varchar(255) NOT NULL,
									  `value` text NOT NULL,
									  `type` varchar(255) NOT NULL default 'system',
									  `type_id` int(11) default NULL,
									  PRIMARY KEY  (`id`)
									) ENGINE=MyISAM
									";
			$sqls['settings'][] = "insert into `".$db->table('settings')."` values
									 ('1','charset','".$charset."','system',null),
									 ('2','site-title','".$site_title."','system',null),
									 ('3','base-url','".$base_url."','system',null),
									 ('4','site-url','".$site_url."','system',null),
									 ('5','uploads-path','/uploads','system',null),
									 ('6','modules-path','/modules','system',null),
									 ('7','plugins-path','/plugins','system',null),
									 ('8','datetime','d-m-Y H:i','system',null),
									 ('9','path-splitter','-','system',null);
									";
		
		// EXECUTE QUERIES
			foreach($sqls as $k => $v) {
				foreach($v as $i => $sql) {
					mysql_query($sql) or $cosa->kill('Oops. An error occured in the installation process!<br />Sorry for that!<br /><br />(ERR_'.strtoupper($k).'_'.($i+1).')<br /><!-- '.mysql_error().' - '.$sql.' -->');
				}
			}
		
		// THANKS
			include ROOT_PATH.'/kernel/lib/tpl/install/thanks.php';
		
	}
?>